package com.yilin.medical.activity;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.yilin.medical.R;
import com.yilin.medical.adapter.SimpleArrayAdapter;
import com.yilin.medical.fragment.KeChengFragment;
import com.yilin.medical.fragment.YiYuanFragment;
import com.yilin.medical.fragment.ZhuanJiaFragment;
import com.yilin.medical.http.AsyTask;
import com.yilin.medical.lsh.BaseResult;
import com.yilin.medical.lsh.Course;
import com.yilin.medical.lsh.Expert;
import com.yilin.medical.lsh.Hospital;
import com.yilin.medical.lsh.Http;
import com.yilin.medical.lsh.Post6_1;
import com.yilin.medical.lsh.SearchResult;
import com.yilin.medical.utils.Inject;
import com.yilin.medical.utils.InjectUtils;
import com.yilin.medical.utils.PromptManager;
import com.yilin.medical.views.RadioGroupContain;

public class KeChengActivity extends BaseActivity implements
		OnPageChangeListener {

	@Inject(R.id.iv_personalcenter)
	private ImageView mImageViewPersonalCenter;
	@Inject(R.id.iv_message)
	private ImageView mImageViewMessage;
	@Inject(R.id.iv_more)
	private ImageView mImageViewMore;
	@Inject(R.id.tv_personalcenter)
	private TextView mTextViewPersonalCenter;
	@Inject(R.id.tv_message)
	private TextView mTextViewMessage;
	@Inject(R.id.tv_more)
	private TextView mTextViewMore;
	@Inject(R.id.ll_personalcenter)
	private LinearLayout mLinearLayoutPersonalCenter;
	@Inject(R.id.ll_message)
	private LinearLayout mLinearLayoutMessage;
	@Inject(R.id.ll_more)
	private LinearLayout mLinearLayoutMore;
	@Inject(R.id.vipager)
	private ViewPager mViewPager;
	private FragmentPagerAdapter mAdapter;
	private List<Fragment> mDatas;
	private RadioGroupContain contain;
	private String[] name = new String[] { "肿瘤", "心血管", "眼科", "消化", "内分泌", "更多" };

	// 是那个分类 课程，会议,指南
	private String type;
	// 那个科室 "肿瘤", "心血管", "眼科", "消化", "内分泌", "更多"
	private String type_class;
	
	
	@Inject(R.id.mlistview)
	private ListView listview;

	private int currenTid;

	// 判断当前网络访问地址是 课程， 专家，医院
	private int currentType =2;
	// 判断当前所属模块  课程 会议 更多
	private int currentModel=1;
	
	private int pagesize = 100;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentViewWithTitle(R.layout.activity_kecheng);
		InjectUtils.inject(this);
		type = getIntent().getStringExtra(HomeActivity.TYPE_KEY);
		type_class = getIntent().getStringExtra(
				KeChengHomeActivity.DEPARTMENT_KEY);
		currenTid = getTidByString(type_class);
		contain = (RadioGroupContain) findViewById(R.id.radio_contain);
		List<String> mlist = new ArrayList<String>();

		for (String str : name) {
			mlist.add(str);
		}
		contain.init(this, mlist);
		initView();
		initData();
		registerLintener();
	}

	private void initView() {
		if (type.equalsIgnoreCase(HomeActivity.TYPE_COURSE)) {
			mTitle.getTitle().setText("课程");
			currentModel = 1;
		} else if (type.equalsIgnoreCase(HomeActivity.TYPE_MEETING)) {
			mTitle.getTitle().setText("会议");
			currentModel=2;
		} else if (type.equalsIgnoreCase(HomeActivity.TYPE_GUIDE)) {
			mTitle.getTitle().setText("指南");
			currentModel = 3;
		}
		contain.getIndex(currenTid-25).setChecked(true);
		
	}

	private void registerLintener() {
		mViewPager.setOnPageChangeListener(this);
		mLinearLayoutPersonalCenter.setOnClickListener(this);
		mLinearLayoutMessage.setOnClickListener(this);
		mLinearLayoutMore.setOnClickListener(this);
		contain.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				PromptManager.showToast(KeChengActivity.this, (checkedId % 6)
						+ "");
				currenTid = checkedId%6+24;
				initHttpData( 0, pagesize);
			}
		});
	}

	private void initData() {
		mDatas = new ArrayList<Fragment>();

		KeChengFragment tab01 = new KeChengFragment();
		ZhuanJiaFragment tab02 = new ZhuanJiaFragment();
		YiYuanFragment tab03 = new YiYuanFragment();

		mDatas.add(tab01);
		mDatas.add(tab02);
		mDatas.add(tab03);

		mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
			@Override
			public int getCount() {
				return mDatas.size();
			}

			@Override
			public Fragment getItem(int arg0) {
				return mDatas.get(arg0);
			}
		};
		mViewPager.setAdapter(mAdapter);
		initHttpData(0,pagesize);
	}

	@SuppressWarnings("unchecked")
	public void initHttpData( int page,int pagesize) {
		Post6_1 post = new Post6_1();
		post.set_tid(currenTid);
		post.setPage(page);
		post.setSize(pagesize);
		post.setOrderby(0);
		Map<String, Object> params = post.toMap(post);
		//课程
		if(currentType==1){
			
			Type type = new TypeToken<BaseResult<SearchResult<Course>>>() {
			}.getType();
		
			/*获取专家。。。*/
			new AsyTask<BaseResult<SearchResult<Course>>>(KeChengActivity.this,
					Http.HttpApis.course, type, true) {

				@Override
				protected void onPost(BaseResult<SearchResult<Course>> result) {
					// TODO Auto-generated method stub
					removeProgressDialog();
					if (result.getSimpleException() != null) {
						PromptManager.showToast(KeChengActivity.this, result
								.getSimpleException().getMessage());
					} else {
						// 初始化 ListView
						initListView(result.getData().getRs());
					}
				}
			}.execute(params);
		}
		//专家
		if(currentType==2){
			Type type = new TypeToken<BaseResult<SearchResult<Expert>>>() {
			}.getType();
		
			/*获取专家。。。*/
			new AsyTask<BaseResult<SearchResult<Expert>>>(KeChengActivity.this,
					Http.HttpApis.expert, type, true) {

				@Override
				protected void onPost(BaseResult<SearchResult<Expert>> result) {
					// TODO Auto-generated method stub
					removeProgressDialog();
					if (result.getSimpleException() != null) {
						PromptManager.showToast(KeChengActivity.this, result
								.getSimpleException().getMessage());
					} else {
						// 初始化 ListView
						initListView(result.getData().getRs());
					}
				}
			}.execute(params);
		}
		
		//医院
		if(currentType==3){
			Type type = new TypeToken<BaseResult<SearchResult<Hospital>>>() {
			}.getType();
		
			/*获取专家。。。*/
			new AsyTask<BaseResult<SearchResult<Hospital>>>(KeChengActivity.this,
					Http.HttpApis.hospital, type, true) {

				@Override
				protected void onPost(BaseResult<SearchResult<Hospital>> result) {
					// TODO Auto-generated method stub
					removeProgressDialog();
					if (result.getSimpleException() != null) {
						PromptManager.showToast(KeChengActivity.this, result
								.getSimpleException().getMessage());
					} else {
						// 初始化 ListView
						initListView(result.getData().getRs());
					}
				}
			}.execute(params);
		}
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void initListView(List rs) {
//		SimpleArrayAdapter<Expert> adapter = new SimpleArrayAdapter<Expert>(KeChengActivity.this, rs, R.layout.listitem_kecheng, new String[]{"pic","price","name","introduction"},
//				new int[]{R.id.iv,R.id.tv_price,R.id.title,R.id.tv_describe}, listview);
//		listview.setAdapter(adapter);
		// 专家
		if(rs==null||rs.size()==0) return;
		 if(rs.get(0) instanceof Expert){
			 SimpleArrayAdapter<Expert> adapter = new SimpleArrayAdapter<Expert>(KeChengActivity.this, rs, R.layout.listitem_zhuanjia, new String[]{"picUrl","name","hospitalName","content","title","sector"},
						new int[]{R.id.iv,R.id.title,R.id.tv_hospitalname,R.id.tv_describe,R.id.tv_zhiwei,R.id.tv_keshi}, listview);
						listview.setAdapter(adapter);
		 }else  if(rs.get(0) instanceof Hospital){
			 SimpleArrayAdapter<Hospital> adapter = new SimpleArrayAdapter<Hospital>(KeChengActivity.this, rs, R.layout.listitem_yiyuan, new String[]{"picUrl","name","introduction"},
						new int[]{R.id.iv,R.id.title,R.id.tv_describe}, listview);
						listview.setAdapter(adapter);
		 }else if(rs.get(0) instanceof Course){
			 SimpleArrayAdapter<Course> adapter = new SimpleArrayAdapter<Course>(KeChengActivity.this, rs, R.layout.listitem_yiyuan, new String[]{"picUrl","name","hospitalName","content","title","sector"},
						new int[]{R.id.iv,R.id.title,R.id.tv_hospitalname,R.id.tv_describe,R.id.tv_zhiwei,R.id.tv_keshi}, listview);
						listview.setAdapter(adapter);
		 }
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
		case R.id.ll_personalcenter:
			mViewPager.setCurrentItem(0, true);
			break;
		case R.id.ll_message:
			mViewPager.setCurrentItem(1, true);
			break;
		case R.id.ll_more:
			mViewPager.setCurrentItem(2, true);
			break;
		}
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
	}

	@Override
	public void onPageSelected(int position) {
		resetTextView();
		switch (position) {
		case 0:
			currentType=1;
			mTextViewPersonalCenter.setTextColor(Color.parseColor("#2B95D3"));
			mImageViewPersonalCenter.setImageResource(R.drawable.kecheng_light);
			break;
		case 1:
			currentType=2;
			mTextViewMessage.setTextColor(Color.parseColor("#2B95D3"));
			mImageViewMessage.setImageResource(R.drawable.zhuanjia_light);
			break;
		case 2:
			currentType=3;
			mTextViewMore.setTextColor(Color.parseColor("#2B95D3"));
			mImageViewMore.setImageResource(R.drawable.yiyuan_light);
			break;
		}
		initHttpData(0,pagesize);
	}

	private void resetTextView() {// #2B95D3蓝色
		mTextViewPersonalCenter.setTextColor(Color.parseColor("#BEB8B8"));
		mImageViewPersonalCenter.setImageResource(R.drawable.kecheng_gray);

		mTextViewMessage.setTextColor(Color.parseColor("#BEB8B8"));
		mImageViewMessage.setImageResource(R.drawable.zhuanjia_gray);

		mTextViewMore.setTextColor(Color.parseColor("#BEB8B8"));
		mImageViewMore.setImageResource(R.drawable.yiyuan_gray);
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		finish();
	}

}
